import time

from selenium.webdriver.common.by import By

from Pages import BasePage


class HotelPage(BasePage.Action):

    def __init__(self):
        #首页酒店
        self.hotelIcon_xpath = (By.XPATH,"//span[text() = '酒店']")
        #酒店关键字
        self.inputHotelName_xpath = (By.XPATH,"//input[@placeholder = '位置/酒店名/品牌']")
        #不限选择
        self.notPrece_xpath = (By.XPATH,"//dd[text() = '不限'][@class = 'unlimit']")
        #酒店目的地
        self.place_xpath = (By.XPATH,"//div[text() = '目的地/地标']/../div/input")
        #热门城市下拉框选苏州
        self.suzhou_xpath = (By.XPATH,"//p[@title = '苏州']")
        #酒店列表页面查看详情
        self.checkDetailPage_xpath = (By.XPATH,"//a[text() = '查看详情']")
        #取消规则：限时取消列表
        self.cancelRule_xpath = (By.XPATH, "//span[contains(@class,'cancel-pop-title')][text()='限时取消']")
        #到店付房型
        self.faceT0Face_xpath = (By.XPATH,"//span[text() = '到店付']")
        #是否有14天疫情区经历“否”
        self.lvxingjingli_loc = (By.CSS_SELECTOR,".icon-xuanzhongyuandian")
        #酒店提交按钮
        self.submitBtn_loc = (By.CSS_SELECTOR,".submit-bar button")
        #重复单仍继续下单
        self.sameOrderCheck_xpath = (By.XPATH, "//div[@class='btn btn-two']")
        #入住日期选择框
        self.datePicker_xpath = (By.XPATH, "//div[@class = 'datepicker']//input")
        #入住日期 = 当前日期+8天，离店日期 = 当前日期+11天
        self.checkInDate = time.strftime('%Y-%m-%d', time.localtime(time.time()+691200))
        self.checkOutDate = time.strftime('%Y-%m-%d', time.localtime(time.time()+950400))
        self.checkInDate_xpath = (By.XPATH,"//div[@class='hotel-search']//span[contains(@class,'day-cell') and not(contains(@class,'datepicker-item-disabled')) and @data-date='%s']" % self.checkInDate)
        self.checkOutDate_xpath = (By.XPATH,"//div[@class='hotel-search']//span[contains(@class,'day-cell') and not(contains(@class,'datepicker-item-disabled')) and @data-date='%s']" % self.checkOutDate)
        #热门城市--苏州
        self.hotCity_Suzhou_xpath = (By.XPATH,"//li[@title='苏州']")
    #点击提交
    def click_submit(self):
        self.smart_click(*self.submitBtn_loc)
        time.sleep(5)
        if self.find_element(*self.sameOrderCheck_xpath):
            self.smart_click(*self.sameOrderCheck_xpath)
        time.sleep(3)

    #点击疫区经验选择
    def click_lvxingjingli(self):
        self.js_click(self.find_elements(*self.lvxingjingli_loc)[1])
        time.sleep(1)
    #点击到店付房型
    def click_faceToFace(self):
        time.sleep(3)
        self.js_click(self.find_elements(*self.faceT0Face_xpath)[0])
        time.sleep(1)
    #点击查看详情
    def click_checkDetail(self):
        self.smart_click(*self.checkDetailPage_xpath)
        time.sleep(2)
        self.switch_window()

    #首页点击酒店
    def click_hotelIcon(self):
        self.smart_click(*self.hotelIcon_xpath)
        time.sleep(0.5)
    #输入酒店名称
    def input_HotelName(self,name):
        self.find_elements(*self.inputHotelName_xpath)[0].send_keys(name)
        time.sleep(2)
    #点击不限
    def click_noPrece(self):
        self.smart_click(*self.notPrece_xpath)

    def input_place(self,name = '苏州'):
        self.find_elements(*self.place_xpath)[0].clear()
        time.sleep(1)
        self.find_elements(*self.place_xpath)[0].send_keys(name)
        self.smart_click(*self.suzhou_xpath)
        time.sleep(2)

    def choosePolicy(self):
        time.sleep(10)
        if self.find_elements(*self.cancelRule_xpath):
            bookButton = (By.XPATH, "//span[contains(@class,'cancel-pop-title')][text()='限时取消']/../../../../dd[@class='operate']")
            self.find_elements(*bookButton)[0].click()
            time.sleep(5)
        else:
            self.click_faceToFace()
            time.sleep(2)


    def chooseDate(self):
        time.sleep(1)
        if self.find_elements(*self.checkInDate_xpath)[0]:
            self.find_elements(*self.checkInDate_xpath)[0].click()
            time.sleep(1)
            self.find_elements(*self.checkOutDate_xpath)[1].click()
            time.sleep(1)
        else:
            self.find_elements(*self.datePicker_xpath)[0].click()
            self.find_elements(*self.checkInDate_xpath)[0].click()
            time.sleep(1)
            self.find_elements(*self.checkOutDate_xpath)[1].click()
            time.sleep(1)

    def chooseCity(self):
        self.find_elements(*self.place_xpath)[0].click()
        time.sleep(1)
        self.smart_click(*self.hotCity_Suzhou_xpath)
        time.sleep(1)